package com.itheima.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.StatusConstant;
import com.itheima.exception.DeletionNotAllowedException;
import com.itheima.goods.dto.SetmealDTO;
import com.itheima.goods.dto.SetmealPageQueryDTO;
import com.itheima.goods.mapper.DishMapper;
import com.itheima.goods.mapper.SetmealDishMapper;
import com.itheima.goods.mapper.SetmealMapper;
import com.itheima.goods.pojo.Setmeal;
import com.itheima.goods.pojo.SetmealDish;
import com.itheima.goods.service.SetmealService;
import com.itheima.goods.vo.DishItemVO;
import com.itheima.goods.vo.SetmealVO;
import com.itheima.result.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageBean pageSelect(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmeals=setmealMapper.pageSelect(setmealPageQueryDTO);
        return new PageBean(setmeals.getTotal(),setmeals.getResult());
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmeals:list",allEntries = true)
    public void update(Integer status, Long id) {
        //停售套餐直接停售
        if (status == StatusConstant.DISABLE) {
            setmealMapper.updateStatus(status, id);
        } else {
            setmealMapper.updateStatus(status, id);
            //起售的话他的菜品都要起售
           List<Long> dishIds= setmealDishMapper.selectById(id);
            for (Long dishId : dishIds) {
                dishMapper.updateStatus(status,dishId);
            }
        }
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmeals:list",allEntries = true)
    public void deleteByIds(Long[] ids) {
        //检查一下你要删除的套餐是否有停售
        Long count= setmealMapper.select(ids);
        if (count> StatusConstant.DISABLE){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        List<Long> deletedIds=setmealMapper.deleteByIds(ids);
        setmealMapper.delete(ids);
        for (Long deletedId : deletedIds) {
            setmealMapper.deleteByIdSetmealDish(deletedId);
        }
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmeals:list",allEntries = true)
    public void addSetneal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);
        Long id = setmeal.getId();//主键自增返回
        System.out.println(id+"==============");
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
            setmealMapper.addSetmealDish(setmealDish);
        }
    }

    @Override
    public SetmealDTO findById(Long id) {
       SetmealDTO setmealDTO= setmealMapper.findById(id);
        System.out.println(setmealDTO+"==========================");
        return setmealDTO;
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmeals:list",allEntries = true)
    public void updateSetmeal(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        setmealMapper.deleteByIdSetmealDish(setmealVO.getId());
        Long id = setmealVO.getId();
        for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
                setmealMapper.addSetmealDish(setmealDish);
        }
    }
//===========================================
    @Override
    @Cacheable(cacheNames = "setmeals:list",key = "#categoryId",unless = "#result.size()<=0")
    public List<Setmeal> list(Long categoryId) {
       List<Setmeal> setmeals= setmealMapper.list(categoryId);

        return setmeals;
    }

    @Override
    public List<DishItemVO> selectById(Long id) {
       List<DishItemVO> dishItemVOS= setmealDishMapper.findById(id);
        return dishItemVOS;
    }
}
